const { ClockInLogUrl, HabitUrl } = require("../../../config/config")

// pages/habit/clockinLog/clockinLog.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clockinLog:{
      hid:'',
      uid:'',
      content:'',
      imageUrl:'',
    },
    habitId:'',
    logContent:'',
    imagePath:'',
  },
  onLoad: function (options) {
    this.setData({
      habitId:options.hid,
    })
  },
  uploadImage:function(){
    let _this = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: ClockInLogUrl+'/uploadImage',
          filePath: tempFilePaths[0],
          name: 'file',
          success (res){
            _this.setData({
              imagePath : res.data
            })
          }
        })
      }
    })
  },
  bindFormSubmit:function(e){
    let _this = this
    let logContent = e.detail.value.logContent
    if(logContent == ''){
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        duration: 1000
      });  
    }else{
      // this.data.clockinLog['createTime'] = new Date
      this.setData({
        'clockinLog.hid' :　_this.data.habitId,
        'clockinLog.uid' : wx.getStorageSync('userinfo').id,
        'clockinLog.content' : logContent,
        'clockinLog.imageUrl' : _this.data.imagePath
      })
      wx.request({
        url:  ClockInLogUrl,
        data: _this.data.clockinLog,
        method: 'POST',
        success: function(res){
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 1000
          });
          wx.redirectTo({
            url: '/pages/habit/clockin/clockin?hid='+_this.data.habitId,
          })
        }
      })
    }
  },


})